package com.xiguaji.tiktok.v1.web.ins;

import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.favorite.dto.FavoriteTypeEnum;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.service.InsFavoriteService;
import com.xiguaji.tiktok.v1.ins.product.dto.InsProductData;
import com.xiguaji.tiktok.v1.ins.product.dto.InsProductSearchRequest;
import com.xiguaji.tiktok.v1.ins.product.service.InsProductService;
import com.xiguaji.tiktok.v1.product.dto.PlatformTypeEnum;
import com.xiguaji.tiktok.v1.product.dto.ReportTypeEnum;
import com.xiguaji.tiktok.v1.user.dto.UserReturnDto;
import com.xiguaji.tiktok.v1.web.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
public class InsProductController extends InsBaseController {

    @Autowired
    InsProductService productService;
    @Autowired
    com.xiguaji.tiktok.v1.product.service.ProductService productPreService;
    @Autowired
    InsFavoriteService insFavoriteService;

    /**
     * 商品搜索
     * @return
     */
    @RequestMapping(value = "/product-search",method = RequestMethod.GET)
    public ModelAndView search(){
        ModelAndView modelAndView = getModel(Tab.INS_PRODUCT_SEARCH.getValue());
        InsProductSearchRequest insProductSearchRequest = new InsProductSearchRequest();
        insProductSearchRequest.setSort(2);
        Page<InsProductData> insProductDataPage = productService.getPage(insProductSearchRequest);

        if(null != getUser()) {
            insProductDataPage.getList().forEach( productData ->{
                InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_PRODUCT.getCode(),productData.getProductId(),(int)getUser().getId());

                if(null != insFavorite) {
                    productData.setCollectId(insFavorite.getId());
                }
            });
        }
        modelAndView.addObject("listData",insProductDataPage);
        modelAndView.addObject("year", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy")));
        modelAndView.setViewName("ins/product/index");
        return modelAndView;
    }

    @RequestMapping(value = "/product/{productId}",method = RequestMethod.GET)
    public ModelAndView productDetail(@PathVariable long productId){
        UserReturnDto userReturnDto = getUserReturn();
        InsProductData insProductData = productService.getProductByProductId(productId);
        ModelAndView modelAndView = getModel(Tab.INS_PRODUCT_SEARCH.getValue());
        modelAndView.setViewName("ins/product/detail/index");
        boolean isUnlock = false;
        Integer collectId = null;

        if(null != userReturnDto){
            isUnlock = productPreService.isUnlocked(getUser().getId(),productId, PlatformTypeEnum.INS.getCode());
            long roleId = userReturnDto.getRoleId();
            InsFavorite insFavorite = insFavoriteService.getByTypeAndTargetIdAndUserId(FavoriteTypeEnum.INS_PRODUCT.getCode(),insProductData.getProductId(),(int)getUser().getId());

            if(null != insFavorite) {
                collectId = insFavorite.getId();
                insProductData.setCollectId(collectId);
            }

            if(!isUnlock){
                // 角色剩余次数
                int unlockCount = productPreService.unlockedNumberByRole(userReturnDto.getId(),roleId, ReportTypeEnum.PRODUCT.getMsg());
                // 报告次数
                if(unlockCount < 1){
                    unlockCount = productPreService.unlockedNumberByReportStarter(userReturnDto.getId());
                }
                // 角色解锁
                if(unlockCount > 0){
                    isUnlock = productPreService.unlockedByUserRole(userReturnDto.getId(),roleId,productId,PlatformTypeEnum.INS.getCode(),ReportTypeEnum.PRODUCT.getCode(),ReportTypeEnum.PRODUCT.getMsg());
                    modelAndView.addObject("user",userReturnDto);
                }
                // 报告解锁
                /*if(!isUnlock){
                    isUnlock = productPreService.unlockedByReportStarter(userReturnDto.getId(),productId, PlatformTypeEnum.INS.getCode(),ReportTypeEnum.PRODUCT.getCode());
                }*/
            }
        }
        modelAndView.addObject("updateTime",productService.getLastStTime(productId)*1000);
        modelAndView.addObject("collectId",collectId);
        modelAndView.addObject("isUnlocked",isUnlock);
        modelAndView.addObject("product",insProductData);
        modelAndView.addObject("myController",this);
        return modelAndView;
    }
}
